package data;
import domain.Actividad;
import java.sql.*;
import java.util.List;
import java.util.LinkedList;

public class JDBCActividadDAO implements IActividadDAO{
	
	public List selectAllActividades (){
		
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		ResultSet result = null;
		List listaActividades = new LinkedList();
		String sql = "SELECT * FROM Actividad ";
		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			while (result.next()) {
				Actividad actividad = rellenarActividad(result);
				listaActividades.add(actividad);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return listaActividades;
	}

	public Actividad select(String nombre){
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		ResultSet result = null;
		Actividad actividad=null;
		String sql = "SELECT * FROM Actividad WHERE (nombre = ?) ";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, nombre);
			result = stmt.executeQuery();
			while(result.next()){
				actividad = rellenarActividad(result);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return actividad;
	}
	
	public void delete(Actividad actividad) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		String sql = "DELETE FROM Actividad WHERE (OID = ?) ";
		try {	
			stmt = conn.prepareStatement(sql);
            stmt.setString(1, actividad.getActividadOID());
            stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
	}

	public void insert(Actividad actividad) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		String sql = "INSERT INTO Actividad (OID ,NOMBRE ,DESCRIPCION ) VALUES (?, ?, ?)";
		try {	
			stmt = conn.prepareStatement(sql);
            stmt.setString(1, actividad.getActividadOID());
            stmt.setString(2, actividad.getNombre());
            stmt.setString(3, actividad.getDescripcion());
            stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
	}
	
	private Actividad rellenarActividad(ResultSet result) throws SQLException {
		Actividad actividad = new Actividad();
		actividad.setActividadOID(result.getString("OID"));
		actividad.setNombre(result.getString("NOMBRE"));
		actividad.setDescripcion(result.getString("DESCRIPCION"));
		return actividad;
	}
	
}



